<?php
namespace Dashboard\Controller;

use Think\Controller;
use Think\Session\Driver\Memcache;

class ProblemsController extends BaseController
{
    public function index()
    {
        $a = (int)($_POST["currpage"]);//跳转页数
        if ($a != null) {
            $currpage = $a;
        } else {
            $currpage = isset($_GET['currpage']) ? $_GET['currpage'] : 1;
        }
        $totalpage = 0;//总页数
        $pagesize = 10;//每页显示数
        //搜索
        $sou = $_POST['sou'];
        $content = $_POST['keywords'];
        if ($sou != null) { //标题
            $currpage = 1;
            $totalpage = 1;//总页数
            if ($sou == 1) {
                $list = sels("problems", "title like '%{$content}%'");
            }
            if ($sou == 2) {
                $list = sels("problems", "jianjie like '%{$content}%'");
            }
            if ($sou == 3) {
                $list = sels("problems", "content like '%{$content}%'");
            }
        } else {
            $arr = currpage("problems", "", "$pagesize", "$currpage", "id desc");
            $totalpage = $arr['totalpage'];
            $list = $arr['list'];
        }
        $this->assign("list", $list)->assign("currpage", $currpage)->assign("totalpage", $totalpage);
        $this->display();
    }

    public function form()
    {
        $id = I('id');
        if ($id != null) {
            $list = sel("problems", "", array("id" => $id));
            $this->assign("list", $list);
        }
        $this->display();
    }
    //存储题目名称
    public function add(){
        $id      = I('id');
        $title   = I('title');
        $content = I('content');
        if($title && $content){
            $data['title']   = $title;
            $data['content'] = $content;
            if($id){
                $data['up_at'] = time();
                $res = up_one("problems","id = {$id}",$data);
            }else{
                $data['create_at'] = time();
                $res = ins("problems",$data);
            }
            if($res){
                $this->ajaxReturn(array("status"=>2));
            }else{
                $this->ajaxReturn(array("status"=>3));
            }
        }else{
            $this->ajaxReturn(array("status"=>1));
        }
    }
    public function yue()
    {
        $a = (int)($_POST["currpage"]);//跳转页数
        if ($a != null) {
            $currpage = $a;
        } else {
            $currpage = isset($_GET['currpage']) ? $_GET['currpage'] : 1;
        }
        $totalpage = 0;//总页数
        $pagesize = 10;//每页显示数
        //搜索
        $sou = $_POST['sou'];
        $content = $_POST['keywords'];
        if ($sou != null) { //标题
            $currpage = 1;
            $totalpage = 1;//总页数
            if ($sou == 1) {
                $list = sels("problems_yue", "name like '%{$content}%'");
            }
            if ($sou == 2) {
                $list = sels("problems_yue", "phone like '%{$content}%'");
            }
        } else {
            $arr = currpage("problems_yue", "", "$pagesize", "$currpage", "id desc");
            $totalpage = $arr['totalpage'];
            $list = $arr['list'];
        }
        $this->assign("list", $list)->assign("currpage", $currpage)->assign("totalpage", $totalpage);
        $this->display();
    }
}